


//时间
var h1 = document.getElementsByClassName('time');
setInterval(function () {
    //获取时间戳函数
    let time2 = Date.now();
    //转换
    var date = new Date(time2);
    //年月日时分秒
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + '-';

    var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':';
    var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());

    var strDate = Y + M + D + h + m + s;
    // console.log(strDate);
    // console.log(h1[0]);
    h1[0].innerHTML = strDate;
}, 1000)

// 点击切换
var lis1 = document.getElementsByClassName('one')
var lis2 = document.getElementsByClassName('two')
var lis3 = document.getElementsByClassName('three')

var div1 = document.getElementsByClassName('first')
var div2 = document.getElementsByClassName('second')
var div3 = document.getElementsByClassName('third')

lis1[0].onclick = function () {
    div1[0].style.display = 'block'
    div2[0].style.display = 'none'
    div3[0].style.display = 'none'
}
lis2[0].onclick = function () {
    div1[0].style.display = 'none'
    div2[0].style.display = 'block'
    div3[0].style.display = 'none'
}
lis3[0].onclick = function () {
    div1[0].style.display = 'none'
    div2[0].style.display = 'none'
    div3[0].style.display = 'block'
}


//获得所有操作的数据
var btns = document.getElementsByClassName('btn-primary'); //获得刷新按钮
var inps = document.getElementsByClassName('wsd'); //获得温湿度框
console.log(inps[0]);
var kt = document.getElementsByName('hot');  //制热制冷单选框
console.log(kt[1]);
console.log(kt.length);
var led = document.getElementsByName('led');  //灯开关
console.log(led[1]);
var huxi = document.getElementsByName('huxi') //呼吸器开关
console.log(huxi[0]);
var look = document.getElementsByName('look')  //检测仪开关
console.log(look[0]);

var led = document.getElementsByName('led')
console.log(led[0]);

//药品柜
var mbbtns = document.getElementsByClassName('modbusbtn');
var mbinps = document.getElementsByClassName('mbwsd'); //获得温湿度框
var mbkt = document.getElementsByName('mbhot');







//点击按钮获取温度湿度重量光照
function sendMbPOSTRequest() {
    var xhr = new XMLHttpRequest(); //创建XMLHTTPRequest对象
    var url = ""; //请求的路径，可以不写
    var data = {
        type: 3,
        key: [6, 7, 13, 14, 15, 17, 18]
    }
    xhr.open("POST", url, true);

    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            var response = xhr.responseText;
            var obj = JSON.parse(response);
            console.log(obj);

            if (obj.type == 3) {
                for (let i = 0; i < obj.data.length; i++) {
                    if (obj.data[i].key == 17) {
                        mbinps[0].value = obj.data[i].val;
                    }
                    else if (obj.data[i].key == 18) {
                        mbinps[1].value = obj.data[i].val;
                    }
                    else if (obj.data[i].key == 6) {
                        mbinps[3].value = obj.data[i].val;
                    }
                    else if (obj.data[i].key == 7) {
                        mbinps[2].value = obj.data[i].val;
                    }
                    else if (obj.data[i].key == 13 && obj.data[i].val == "1") {
                        mbkt[0].checked = true;
                    }
                    else if (obj.data[i].key == 14 && obj.data[i].val == "1") {
                        mbkt[1].checked = true;
                    }
                    else if (obj.data[i].key == 15 && obj.data[i].val == "1") {
                        mbkt[2].checked = true;
                    }
                    else if (obj.data[i].key == 13 && obj.data[i].val == "0") {
                        mbkt[0].checked = false;
                    }
                    else if (obj.data[i].key == 14 && obj.data[i].val == "0") {
                        mbkt[1].checked = false;
                    }
                    else if (obj.data[i].key == 15 && obj.data[i].val == "0") {
                        mbkt[2].checked = false;
                    }
                }
            }
        }
    }
    xhr.send(JSON.stringify(data));
}


//点击按钮，获得数据（温湿度）共享内存
function sendPOSTRequest() {
    var xhr = new XMLHttpRequest(); //创建XMLHTTPRequest对象
    var url = ""; //请求的路径，可以不写
    var data = {
        type: 3,
        key: [0,1,2,3, 4,9,10,11]
    }
    xhr.open("POST", url, true);

    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            var response = xhr.responseText;
            var obj = JSON.parse(response);
            console.log(obj);

            if (obj.type == 3) {
                for (let i = 0; i < obj.data.length + 1; i++) {
                    if (obj.data[i].key == 3) {
                        inps[0].value = obj.data[i].val;
                    }
                    else if (obj.data[i].key == 4) {
                        inps[1].value = obj.data[i].val;
                    }
                    else if (obj.data[i].key == 0 && obj.data[i].val == "1") {
                        kt[0].checked = true;
                    }
                    else if (obj.data[i].key == 1 && obj.data[i].val == "1") {
                        kt[1].checked = true;
                    }
                    else if (obj.data[i].key == 2 && obj.data[i].val == "1") {
                        kt[2].checked = true;
                    }
                    else if (obj.data[i].key == 9 && obj.data[i].val == "1") {
                        led[0].checked = true;
                    }
                    else if (obj.data[i].key == 10 && obj.data[i].val == "1") {
                        huxi[0].checked = true;
                    }
                    else if (obj.data[i].key == 11 && obj.data[i].val == "1") {
                        look[0].checked = true;
                    }
                    else if (obj.data[i].key == 0 && obj.data[i].val == "0") {
                        kt[0].checked = false;
                    }
                    else if (obj.data[i].key == 1 && obj.data[i].val == "0") {
                        kt[1].checked = false;
                    }
                    else if (obj.data[i].key == 2 && obj.data[i].val == "0") {
                        kt[2].checked = false;
                    }
                    else if (obj.data[i].key == 9 && obj.data[i].val == "0") {
                        led[0].checked = false;
                    }
                    else if (obj.data[i].key == 10 && obj.data[i].val == "0") {
                        huxi[0].checked = false;
                    }
                    else if (obj.data[i].key == 11 && obj.data[i].val == "0") {
                        look[0].checked = false;
                    }
                }
            }
        }
    }
    xhr.send(JSON.stringify(data));
}

btns[0].onclick = function () {
    sendPOSTRequest();
}
mbbtns[0].onclick = function () {
    sendMbPOSTRequest()
}
//进入页面时先调用一次，获得一下温湿度
window.onload = sendPOSTRequest();
window.onload = sendMbPOSTRequest();



//空调（32设备）制冷，制热，除湿，关闭
//点击制冷关闭制热和除湿
//点击制热关闭制冷和除湿
//点击除湿关闭制冷和制热
//点击关闭关闭所有
console.log(kt.length);
kt[0].onclick = function kongtiaohot() {
        var xhr = new XMLHttpRequest(); //创建XMLHTTPRequest对象
        var url = ""; //请求的路径，可以不写
        var data = {
            type: 1,
            key: 0,
            val: 1
        }
        xhr.open("POST", url, true);


        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
            }
        }
        xhr.send(JSON.stringify(data));
}


kt[1].onclick = function kongtiaohot() {
    var xhr = new XMLHttpRequest(); //创建XMLHTTPRequest对象
    var url = ""; //请求的路径，可以不写
    var data = {
        type: 1,
        key: 1,
        val: 1
    }
    xhr.open("POST", url, true);


    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
        }
    }
    xhr.send(JSON.stringify(data));
}


kt[2].onclick = function kongtiaohot() {
    var xhr = new XMLHttpRequest(); //创建XMLHTTPRequest对象
    var url = ""; //请求的路径，可以不写
    var data = {
        type: 1,
        key: 2,
        val: 1
    }
    xhr.open("POST", url, true);


    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
        }
    }
    xhr.send(JSON.stringify(data));
}

// kt[1].onclick = function kongtiaocold() {
//     var flex = 0;
//     for (let i = 0; i < kt.length - 1; i++) {
//         var xhr = new XMLHttpRequest(); //创建XMLHTTPRequest对象
//         var url = ""; //请求的路径，可以不写
//         var number = i;
//         if (number == "1") {
//             flex = 1;
//         }
//         var data = {
//             type: 1,
//             key: number,
//             val: flex
//         }
//         xhr.open("POST", url, true);


//         xhr.onreadystatechange = function () {
//             if (xhr.readyState === 4 && xhr.status === 200) {
//             }
//         }
//         console.log(i);

//         xhr.send(JSON.stringify(data));
//         flex = 0;
//     }
// }
// // //除湿
// kt[2].onclick = function kongtiaocs() {
//     var flex = 0;
//     for (let i = 0; i < kt.length - 1; i++) {
//         var xhr = new XMLHttpRequest(); //创建XMLHTTPRequest对象
//         var url = ""; //请求的路径，可以不写
//         var number = i;
//         if (number == "2") {
//             flex = 1;
//         }
//         var data = {
//             type: 1,
//             key: number,
//             val: flex
//         }
//         xhr.open("POST", url, true);


//         xhr.onreadystatechange = function () {
//             if (xhr.readyState === 4 && xhr.status === 200) {
//             }
//         }

//         xhr.send(JSON.stringify(data));
//         console.log(i);
//         flex = 0;
//     }
// }
//关闭
kt[3].onclick = function kongtiaoclose() {
    var flex = 0;

    for (let i = 0; i < kt.length - 1; i++) { 
        setTimeout(function(){
            var xhr = new XMLHttpRequest; //创建XMLHTTPRequest对象
            var url = ""; //请求的路径，可以不写
            var number = i;
            var data = {
                type: 1,
                key: number,
                val: flex
            }
            xhr.open("POST", url, true);
    
    
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                }
            }
            xhr.send(JSON.stringify(data));
        },500)
    }
}


mbkt[0].onclick = function mbkongtiaohot() {
    var flex = 0;
    for (let i = 0; i < mbkt.length - 1; i++) {
        var xhr = new XMLHttpRequest(); //创建XMLHTTPRequest对象
        var url = ""; //请求的路径，可以不写
        var number = i + 13;
        console.log(number);
        if (number == "13") {
            flex = 1;
        }
        var data = {
            type: 1,
            key: number,
            val: flex
        }
        xhr.open("POST", url, true);


        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
            }
        }

        xhr.send(JSON.stringify(data));
        console.log(i);
        flex = 0;
    }
}

mbkt[1].onclick = function mbkongtiaohot() {
    var flex = 0;
    for (let i = 0; i < mbkt.length - 1; i++) {
        var xhr = new XMLHttpRequest(); //创建XMLHTTPRequest对象
        var url = ""; //请求的路径，可以不写
        var number = i + 13;
        console.log(number);
        if (number == "14") {
            flex = 1;
        }
        var data = {
            type: 1,
            key: number,
            val: flex
        }
        xhr.open("POST", url, true);


        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
            }
        }

        xhr.send(JSON.stringify(data));
        console.log(i);
        flex = 0;
    }
}

mbkt[2].onclick = function mbkongtiaohot() {
    var flex = 0;
    for (let i = 0; i < mbkt.length - 1; i++) {
        var xhr = new XMLHttpRequest(); //创建XMLHTTPRequest对象
        var url = ""; //请求的路径，可以不写
        var number = i + 13;
        console.log(number);
        if (number == "15") {
            flex = 1;
        }
        var data = {
            type: 1,
            key: number,
            val: flex
        }
        xhr.open("POST", url, true);


        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
            }
        }

        xhr.send(JSON.stringify(data));
        console.log(i);
        flex = 0;
    }
}

mbkt[3].onclick = function mbkongtiaohot() {
    var flex = 0;

    for (let i = 0; i < mbkt.length - 1; i++) {
        var xhr = new XMLHttpRequest; //创建XMLHTTPRequest对象
        var url = ""; //请求的路径，可以不写
        var number = i + 13;
        var data = {
            type: 1,
            key: number,
            val: flex
        }
        xhr.open("POST", url, true);


        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
            }
        }
        xhr.send(JSON.stringify(data));
        console.log(i);;
    }
}




//呼吸机开关
huxi[0].onclick = function huxiopen() {
    var xhr = new XMLHttpRequest(); //创建XMLHTTPRequest对象
    var url = ""; //请求的路径，可以不写
    var data = {
        type: 1,
        key: 10,
        name: "Ventilators",
        val: 1
    }

    xhr.open("POST", url, true);


    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
        }
    }
    xhr.send(JSON.stringify(data));
}

huxi[1].onclick = function huxiclose() {
    var xhr = new XMLHttpRequest(); //创建XMLHTTPRequest对象
    var url = ""; //请求的路径，可以不写
    var data = {
        type: 1,
        key: 10,
        name: "Ventilators",
        val: 0
    }

    xhr.open("POST", url, true);


    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
        }
    }
    xhr.send(JSON.stringify(data));
}

look[0].onclick = function jisnceopen() {
    var xhr = new XMLHttpRequest(); //创建XMLHTTPRequest对象
    var url = ""; //请求的路径，可以不写
    var data = {
        type: 1,
        key: 11,
        name: "monitor",
        val: 1
    }

    xhr.open("POST", url, true);


    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
        }
    }
    xhr.send(JSON.stringify(data));
}

look[1].onclick = function jisnceclose() {
    var xhr = new XMLHttpRequest(); //创建XMLHTTPRequest对象
    var url = ""; //请求的路径，可以不写
    var data = {
        type: 1,
        key: 11,
        name: "monitor",
        val: 0
    }

    xhr.open("POST", url, true);


    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
        }
    }
    xhr.send(JSON.stringify(data));
}


led[0].onclick = function led_bing() {
    var xhr = new XMLHttpRequest(); //创建XMLHTTPRequest对象
    var url = ""; //请求的路径，可以不写
    var data = {
        type: 1,
        key: 9,
        name: "light_bing",
        val: 1
    }

    xhr.open("POST", url, true);


    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
        }
    }
    xhr.send(JSON.stringify(data));
}

led[1].onclick = function led_bing() {
    var xhr = new XMLHttpRequest(); //创建XMLHTTPRequest对象
    var url = ""; //请求的路径，可以不写
    var data = {
        type: 1,
        key: 9,
        name: "light_bing",
        val: 0
    }

    xhr.open("POST", url, true);


    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
        }
    }
    xhr.send(JSON.stringify(data));
}



